/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 pjoc.pub, blademainer.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package pub.pjoc.pay.threadpool.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pub.pjoc.pay.threadpool.*;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Default implementer.
 *
 * @author blademainer
 * @version 2016-08-31 23:16
 */
public class ThreadPoolSchedulerImpl<T> implements ThreadPoolScheduler {
  private static final Logger logger = LoggerFactory.getLogger(ThreadPoolSchedulerImpl.class);
  private CallableThread<T> callableThread;
  private CallableThreadFactory<T> callableThreadFactory;
  private ThreadPoolContainer threadPoolContainer = new ThreadPoolContainerImpl();
  private boolean useFactory;

  public ThreadPoolSchedulerImpl(CallableThreadFactory<T> callableThreadFactory, ThreadCallbackHandler callbackHandler) {
    useFactory = true;
    this.callableThreadFactory = callableThreadFactory;
    callbackHandler.setThreadPoolScheduler(this);
  }

  public ThreadPoolSchedulerImpl(CallableThread<T> callableThread, ThreadCallbackHandler callbackHandler) {
    useFactory = false;
    this.callableThread = callableThread;
    callbackHandler.setThreadPoolScheduler(this);
  }

  @Override
  public void resetPoolSize(int newSize) {
    threadPoolContainer.resetPoolSize(newSize);
    start();
  }

  @Override
  public void submit() {
    ThreadPoolExecutor threadPool = threadPoolContainer.getThreadPool();
    if (useFactory) {
      CallableThread<T> callableThread = callableThreadFactory.produceCallableThread();
      threadPool.submit(callableThread);
    } else {
      threadPool.submit(callableThread);
    }
  }

  @Override
  public void setThreadPoolContainer(ThreadPoolContainer threadPoolContainer) {
    if (threadPoolContainer == null) {
      logger.error("ThreadPoolContainer is null when setting a new one! so dismiss!");
      return;
    }
    this.threadPoolContainer = threadPoolContainer;
  }

  @Override
  public ThreadPoolContainer getThreadPoolContainer() {
    return threadPoolContainer;
  }

  @Override
  public void start() {
    ThreadPoolExecutor executorService = threadPoolContainer.getThreadPool();
    int maximumPoolSize = executorService.getMaximumPoolSize();
    for (int i = 0; i < maximumPoolSize; i++) {
      submit();
    }
  }
}
